import { login, logout, getInfo } from '@/api/login/login';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { userBaseInfo } from '@/utils/userBaseInfo'

const user = {
	state: {
		token: getToken(),
		name: '',
		avatar: '',
		roles: ''
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token;
		},
		SET_NAME: (state, name) => {
			state.name = name;
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar;
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles;
		}
	},

	actions: {
		// 登录
		// 此action 调用了/src/api/login/login.js 中的方法请求后台接口,用Promise,操作返回的数据
		Login({ commit }, userInfo) {
			const loginName = userInfo.loginName.trim();
			return new Promise((resolve, reject) => {
				login(loginName, userInfo.loginPassword).then(response => {
					const code = response.meta.code;
					if (code == 200) {
						const data = response.data;
						setToken(data.token);
                        commit('SET_TOKEN', data.token);
                        // 设置登录账号
                        userBaseInfo.setLoginName(loginName);
						resolve();
					} else {
						reject();
					}
				}).catch(error => {
					reject(error);
				});
			});
		},


		/**
		 * {
				"meta": {
					"code": 200,
					"message": "success"
				},
				"data": {
					"loginName": "xxx",
					"email": "65333@qq.com",
					"phone": "12345678987",
					"roleName": "超级管理员",
					"rolePermission": "",
					"isSuperAdmin": 1,
					"teamNameSpace": null,
					"teamName": null
				}
			}
		 */
		// 获取用户信息
		GetInfo({ commit, state }) {
			return new Promise((resolve, reject) => {
				if (getToken()) {
					getInfo(getToken()).then(response => {
						const data = response.data;
						commit('SET_ROLES', data.roleName);
						commit('SET_NAME', data.loginName);
						// 将用户信息设置到浏览器的session中
						// console.log('userBaseIfno=' + JSON.stringify(data));
						userBaseInfo.setUserId(data.userId);
						userBaseInfo.setLoginName(data.loginName);
						userBaseInfo.setEmail(data.email);
						userBaseInfo.setPhone(data.phone);
						userBaseInfo.setRoleName(data.roleName);
						userBaseInfo.setRolePermission(data.rolePermission);
						userBaseInfo.setIsSuperAdmin(data.isSuperAdmin);
						userBaseInfo.setUserName(data.userName);
						resolve(response);
					}).catch(error => {
						reject(error);
					});
				}

			});
		},

		// 登出
		LogOut({ commit, state }) {
			return new Promise((resolve, reject) => {
				logout(getToken()).then(response => {
					const code = response.meta.code;
					if (code == 200) {
						commit('SET_TOKEN', '');
						commit('SET_ROLES', []);
						removeToken();
						// 将用户信息设置为空
						userBaseInfo.cleanUserBase();
						resolve();

					} else {
						reject();
					}
				}).catch(error => {
					reject(error);
				});
			});
		},

		// 前端 登出
		FedLogOut({ commit }) {
			return new Promise(resolve => {
				commit('SET_TOKEN', '');
				removeToken();
				// 将用户信息设置为空
				userBaseInfo.cleanUserBase();
				resolve();
			});
		}
	}
};

export default user;
